package dao;

import bean.Log;
import util.Linksql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LogsDaoImpl{
    public List<Log> getLogs(String seat_name) {
        Connection connection = Linksql.getConnection();
        PreparedStatement prep;
        ResultSet resultSet;
        String sql1 = "select * from book_s_st where seatnum='"+seat_name+"' ;";
        String sql2 = "select * from book_s_sn where seminarnum='"+seat_name+"';";
        String sql3 = "select * from book_s_rd where readroomnum='"+seat_name+"';";
        String sql4 = "select * from book_t_sn where seminarnum='"+seat_name+"';";
        String sql5 = "select * from book_t_st where seatnum='"+seat_name+"' ;";
        String sql6 = "select * from book_t_rd where readroomnum='"+seat_name+"';";
        List<Log> list = new ArrayList<>();
        try{
            prep = connection.prepareStatement(sql1);
            resultSet =prep.executeQuery();
            while (resultSet.next()){
                Log log = new Log();
                log.setStart_time(resultSet.getString("starttime"));
                log.setEnd_time(resultSet.getString("endtime"));
                log.setArrivetime(resultSet.getString("arrivetime"));
                log.setActual_end_time(resultSet.getString("actualendtime"));
                log.setSubmit_time(resultSet.getString("submittime"));
                log.setId(resultSet.getString("stuid"));
                log.setNum(resultSet.getString("seatnum"));
                log.setStatus(resultSet.getString("status"));
                list.add(log);
            }
            prep = connection.prepareStatement(sql2);
            resultSet =prep.executeQuery();
            while (resultSet.next()){
                Log log = new Log();
                log.setStart_time(resultSet.getString("starttime"));
                log.setEnd_time(resultSet.getString("endtime"));
                log.setArrivetime(resultSet.getString("arrivetime"));
                log.setActual_end_time(resultSet.getString("actualendtime"));
                log.setSubmit_time(resultSet.getString("submittime"));
                log.setId(resultSet.getString("stuid"));
                log.setNum(resultSet.getString("seminarnum"));
                log.setStatus(resultSet.getString("status"));
                list.add(log);
            }
            prep = connection.prepareStatement(sql3);
            resultSet =prep.executeQuery();
            while (resultSet.next()){
                Log log = new Log();
                log.setStart_time(resultSet.getString("starttime"));
                log.setEnd_time(resultSet.getString("endtime"));
                log.setArrivetime(resultSet.getString("arrivetime"));
                log.setActual_end_time(resultSet.getString("actualendtime"));
                log.setSubmit_time(resultSet.getString("submittime"));
                log.setId(resultSet.getString("stuid"));
                log.setNum(resultSet.getString("readroomnum"));
                log.setStatus(resultSet.getString("status"));
                list.add(log);
            }
            prep = connection.prepareStatement(sql4);
            resultSet =prep.executeQuery();
            while (resultSet.next()){
                Log log = new Log();
                log.setStart_time(resultSet.getString("starttime"));
                log.setEnd_time(resultSet.getString("endtime"));
                log.setArrivetime(resultSet.getString("arrivetime"));
                log.setActual_end_time(resultSet.getString("actualendtime"));
                log.setSubmit_time(resultSet.getString("submittime"));
                log.setId(resultSet.getString("teacherid"));
                log.setNum(resultSet.getString("seminarnum"));
                log.setStatus(resultSet.getString("status"));
                list.add(log);
            }
            prep = connection.prepareStatement(sql5);
            prep.execute();
            resultSet =prep.executeQuery();
            while (resultSet.next()){
                Log log = new Log();
                log.setStart_time(resultSet.getString("starttime"));
                log.setEnd_time(resultSet.getString("endtime"));
                log.setArrivetime(resultSet.getString("arrivetime"));
                log.setActual_end_time(resultSet.getString("actualendtime"));
                log.setSubmit_time(resultSet.getString("submittime"));
                log.setId(resultSet.getString("teacherid"));
                log.setNum(resultSet.getString("seatnum"));
                log.setStatus(resultSet.getString("status"));
                list.add(log);
            }
            prep = connection.prepareStatement(sql6);
            resultSet =prep.executeQuery();
            while (resultSet.next()){
                Log log = new Log();
                log.setStart_time(resultSet.getString("starttime"));
                log.setEnd_time(resultSet.getString("endtime"));
                log.setArrivetime(resultSet.getString("arrivetime"));
                log.setActual_end_time(resultSet.getString("actualendtime"));
                log.setSubmit_time(resultSet.getString("submittime"));
                log.setId(resultSet.getString("teacherid"));
                log.setNum(resultSet.getString("readroomnum"));
                log.setStatus(resultSet.getString("status"));
                list.add(log);
            }
            prep.close();
            resultSet.close();
            connection.close();
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return list;
    }

}
